package com.pie4cloud.pie.job.biz.controller;


import com.pie4cloud.pie.common.core.util.R;
import com.pie4cloud.pie.job.api.dto.*;
import com.pie4cloud.pie.job.api.commons.Constant;
import com.pie4cloud.pie.job.api.entity.PlaybillEntity;
import com.pie4cloud.pie.job.biz.service.PlayBillService;
import com.pie4cloud.pie.job.biz.service.PortalService;
import com.pie4cloud.pie.job.biz.util.ClassUtils;
import com.pie4cloud.pie.job.biz.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/portal")
public class PortalController {
    @Autowired
    private PortalService portalService;

    @Resource
    private PlayBillService playBillService;

	@Value("#{${sod.config.configMapForPlayBill}}")
    private HashMap<String,String> configMapForPlayBill;

	@Value("#{${sod.config.configMapForPlayCommons}}")
	private HashMap<String,String> configMapForPlayCommons;

    @GetMapping(value = "/sendSodGetRequest")
    @ResponseBody
    public String sendSodGetRequest(DataRegister dataRegister) {
        String str=portalService.sendSodGetRequest(dataRegister);
        return str;
    }
    @RequestMapping(value = "/sendSodPostRequest", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String sendSodPostRequest(@RequestBody DataRegister dataRegister) {
        String str=portalService.sendSodPostRequest(dataRegister);
        return str;
    }

	@GetMapping(value = "/getTableInfoByPlayBillId")
	@ResponseBody
	public R getTableInfoByPlayBillId(Long playBillId) {
		try {
			PlaybillEntity playbillEntity = playBillService.getById(playBillId);
			if (playbillEntity == null) {
				return R.failed("未找到对应节目单信息");
			}
			String tableName = playbillEntity.getTableName();
			String databaseId = playbillEntity.getDatabaseId();
			List<TableDecodeMap> list = new ArrayList<TableDecodeMap>();
			if(StringUtils.contains(tableName,".")) {
				tableName = StringUtils.split(tableName,".")[1];
			}
			SodCallResult<TableDetailInfo> sodCallResult = portalService.getTableInfoByDatabaseIdAndTableName(databaseId, tableName);
			if(Constant.SOD_CALL_SUSSESS_CODE.equals(sodCallResult.getCode())) {
				TableDetailInfo data = sodCallResult.getData();
				List<TableColumns> columns = data.getColumns();
				for(int i = 0; i < columns.size();i++) {
					TableColumns tableColumns = columns.get(i);
					TableDecodeMap tableDecodeMap = new TableDecodeMap();
					tableDecodeMap.setColName(tableColumns.getEleName());
					String dbEleCode = tableColumns.getDbEleCode();
					tableDecodeMap.setColCode(dbEleCode);
					if (configMapForPlayBill != null && configMapForPlayBill.containsKey(dbEleCode)) {
						tableDecodeMap.setDecodeConfig(ClassUtils.getClassValueByEntityAndName(playbillEntity,configMapForPlayBill.get(dbEleCode)));
					}else if(configMapForPlayCommons != null && configMapForPlayCommons.containsKey(dbEleCode)) {
						tableDecodeMap.setDecodeConfig(configMapForPlayCommons.get(dbEleCode));
					}
					list.add(tableDecodeMap);
				}
				return R.ok(list);
			}
		} catch (Exception e) {
			return R.failed();
		}
		return R.failed();
	}
}
